import {Component} from '@angular/core';
import {FormControl, FormBuilder, FormGroup, Validators} from '@angular/forms';
import {NavController, NavParams} from 'ionic-angular';
import {ControlsCheck} from '../../common/controlsCheck';
import {CONST} from '../../common/const';
import {Helper} from "../../common/Helper";
import {RegisterProvider} from "../../providers/register/register";

@Component({
  selector: 'page-register',
  templateUrl: 'register.html'
})
export class RegisterPage {

  phone: FormControl
  form: FormGroup
  canSub: Boolean = true
  canSendCode: Boolean = true
  sendTime: number = CONST.CODE_TIME
  sendTimer: any
  bRegister: boolean = false

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private cc: ControlsCheck,
              private helper: Helper,
              private registerProvider: RegisterProvider,
              private fb: FormBuilder) {
    this.createForm()
    // this.helper.loading()
  }

  createForm() {
    this.form = this.fb.group({
      cellphone: ['', Validators.compose([Validators.required, Validators.pattern(CONST.REG_PHONE)])],
      signPassword: ['', Validators.compose([Validators.required, Validators.pattern(CONST.REG_PASSWORD)])],
      verificationCode: ['', Validators.compose([Validators.required])],
    })
  }

  //检测手机号是否已注册
  checkPhone() {
    let cellphoneCtrl = this.form.controls.cellphone
    if (cellphoneCtrl.valid) {
      this.registerProvider.checkPhone({
        data: {
          cellphone: cellphoneCtrl.value
        },
        success: () => {
          this.bRegister = false
        },
        fail: (err) => {
          if (err.code == '80003') {
            this.bRegister = true
            this.initTimer()
          }
        }
      })
    }
  }

  sendCode() {
    let cellphoneCtrl = this.form.controls.cellphone
    if (cellphoneCtrl.invalid) {
      this.cc.toastMsg('请输入正确的手机号')
      return
    }
    if (this.bRegister) {
      this.cc.toastMsg('该手机号码已经注册了')
      return
    }
    if (!this.canSendCode) return

    this.cc.toastMsg('验证码已发送，请注意查收')
    this.canSendCode = false

    this.registerProvider.getVerityCode({
      data: {
        cellphone: cellphoneCtrl.value
      },
      success: () => {
        this.sendTimer = setInterval(() => {
          this.sendTime--
          if (this.sendTime < 0) {
            this.initTimer()
          }
        }, 1000)
      }
    })

  }

  initTimer() {
    clearInterval(this.sendTimer)
    this.canSendCode = true
    this.sendTime = CONST.CODE_TIME
  }

  submit() {
    if (this.form.valid) {
      if (this.canSub) {
        this.canSub = false
        this.registerProvider.register({
          data: this.form.value,
          complete: () => {
            this.canSub = true
          },
          success: () => {
            this.helper.alert({
              title: '注册成功',
              cssClass: 'g-alert-custom',
              enableBackdropDismiss: false,
              buttons: [{
                text: '立即登录',
                handler: () => {
                  this.navCtrl.pop()
                }
              }]
            })
          }
        })
      }
      else return false
    } else {
      this.cc.check(this.form.controls, {
        cellphone: '请输入正确的手机号',
        signPassword: '密码需由6-20位数字、字母组合',
        verificationCode: '请输入正确的验证码'
      })
    }
  }


}
